<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2021/7/5
 * Time: 15:30
 */

namespace app\api\controller;
use Qcloud\Cos\Client;
require_once "../extend/tencent/vendor/autoload.php";

class Pdftoimg extends Base
{
    // 安装ghostscript yum install -y ghostscript
    public function toImgUploadQn($filename,$page = -1){
        set_time_limit(0);
        $return_url = '';
        try{
            $im = new \Imagick();
            $im->setResolution(120, 120);
            $im->setCompressionQuality(100);
            if($page == -1){
                $im->readImage($filename);
            }else{
                $im->readImage($filename."[".$page."]");
            }
            $Return = [];
            $path = ROOT_PATH . 'public';

            foreach ($im as $Key => $Var){
                $Var->setImageFormat('png');
                $key = md5($Key.time()) . '.png';
                $file_name = $path . DS . 'uploads/imagick/' . $key;
                if($Var->writeImage($file_name) == true){
                    $configconfig = [
                        'region' => config('tengx')['region'],
                        'schema' => config('tengx')['schema'], //协议头部，默认为http
                        'credentials'=> array(
                            'secretId'  => config('tengx')['secretId'] ,
                            'secretKey' => config('tengx')['secretKey']
                        )
                    ];
                    $cosClient = new Client($configconfig);
                    $filePath = $file_name;
                    $result = $cosClient->putObject(array(
                        'Bucket' => config('tengx')['Bucket'], //格式：BucketName-APPID
                        'Key' => $key,
                        'Body' => fopen($filePath, 'rb'),
                    ));
                    // 请求成功
                    $data = config('tengx')['domain'] . $key;
                    array_push($Return, $data);
                    @unlink($file_name);
                }
            }
            @unlink($filename);
            if($Return){
                $return_url = implode(',', $Return);
            }
            return $return_url;
        }catch(\Exception $e){
            return $e->getMessage();
        }
    }
}